package com.chat.business;

import java.util.Date;

import com.chat.data.MessageItem;
import com.chat.util.ChatConstants;
import com.uday.frmwrk.data.BaseRequestItem;
import com.uday.frmwrk.data.BaseResponseItem;
import com.uday.frmwrk.service.FlexSessionActivity;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.services.MessageService;
import flex.messaging.util.UUIDUtils;

public class SendMessageActivity extends FlexSessionActivity {

	public boolean process(BaseRequestItem objBaseRequestItem,
			BaseResponseItem objBaseResponseItem) throws Exception {
		boolean blnRetVal = false;
		
		MessageItem objMessageItem = (MessageItem) objBaseRequestItem
				.getObjReqData();
		MessageBroker broker = MessageBroker.getMessageBroker(null);
		MessageService service = (MessageService) broker
				.getService(ChatConstants.MESSAGE_SERVICE);

		if (broker != null) {
			String clientID = UUIDUtils.createUUID();
			AsyncMessage msg = new AsyncMessage();
			msg.setClientId(clientID);
			msg.setMessageId(UUIDUtils.createUUID());
			msg.setTimestamp(System.currentTimeMillis());

			if (LoginActivity.mapUserIDQue.containsKey(objMessageItem
					.getLngToUserId())) {

				String strMsgDets = LoginActivity.mapUserIDQue
						.get(objMessageItem.getLngToUserId());
				if (service.getDestination(strMsgDets) != null) {
					msg.setDestination(LoginActivity.mapUserIDQue
							.get(objMessageItem.getLngToUserId()));

					msg.setBody(objMessageItem);

					broker.routeMessageToService(msg, null);
					blnRetVal = true;
				}
			}
			System.out.println("Message sent at " + new Date().toString());
		}
		return blnRetVal;
	}
}
